def is_same_line(rect1, rect2):
    """
    判断两个矩形是否在同一行上

    参数:
    rect1: 第一个矩形的坐标列表，形如[[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
    rect2: 第二个矩形的坐标列表，形如[[x1, y1], [x2, y2], [x3, y3], [x4, y4]]

    返回:
    bool: 如果两个矩形在同一行上则返回True，否则返回False
    """
    # 获取前一个矩形的右上点和右下点的y坐标范围
    rect1_right_top_y = rect1[1][1]
    rect1_right_bottom_y = rect1[2][1]

    # 获取后一个矩形的左上点和左下点的y坐标
    rect2_left_top_y = rect2[0][1]
    rect2_left_bottom_y = rect2[3][1]

    # 根据中点判断
    rect2_middle_y = (rect2_left_top_y + rect2_left_bottom_y)/2
    if rect1_right_top_y <= rect2_middle_y <= rect1_right_bottom_y:
        return True
    else:
        return False

    # 判断后一个矩形的左上点或左下点的y坐标是否在前一个矩形的右上点和右下点的y坐标范围内---
    # if rect1_right_top_y <= rect2_left_top_y <= rect1_right_bottom_y or \
    #         rect1_right_top_y <= rect2_left_bottom_y <= rect1_right_bottom_y:
    #     return True
    # elif rect2_left_top_y <= rect1_right_top_y <= rect2_left_bottom_y or \
    #         rect2_left_top_y <= rect1_right_bottom_y <= rect2_left_bottom_y:
    #     return True
    # else:
    #     return False


def rearrange_text_by_line(text_array):
    """
    按照顺时针方向重新排列文本

    参数:
    text_array: 包含矩形坐标和对应文本的数组

    返回:
    new_array: 重新排列后的数组
    """
    # 按照左上角坐标排序
    # text_array.sort(key=lambda x: (x[0][0][0], x[0][0][1]))

    new_array = []
    current_line = []
    current_y_range = None

    for item in text_array:
        rect = item[0]
        text = item[1][0]
        confidence = item[1][1]

        if current_line and not is_same_line(current_line[-1][0], rect):
            new_array.append(current_line)
            current_line = []

        current_line.append((rect, text, confidence))

    if current_line:
        new_array.append(current_line)

    # 按照顺时针方向重新排列文本
    # for line in new_array:
    #     line.sort(key=lambda x: (x[0][0][0], x[0][0][1]))

    return new_array

def ocr_sort(text_array):
    # 测试代码
    # text_array = [
    #     [[[49.0, 50.0], [412.0, 63.0], [411.0, 92.0], [48.0, 79.0]], ('JT2024041003Z目称：宁乡紫云府', 0.9508424401283264)],
    #     [[[477.0, 63.0], [781.0, 60.0], [781.0, 81.0], [477.0, 84.0]],
    #      ('合同来源：战略集采中心材料名称：气体', 0.9967018961906433)],
    #     [[[126.0, 94.0], [287.0, 94.0], [287.0, 115.0], [126.0, 115.0]], ('附件4：采购入库单', 0.9982880353927612)],
    #     [[[542.0, 88.0], [768.0, 85.0], [768.0, 107.0], [542.0, 109.0]], ('附件11：《举报渠道告知函》', 0.9974693655967712)],
    #     [[[127.0, 122.0], [396.0, 118.0], [396.0, 140.0], [127.0, 143.0]],
    #      ('附件5：质量保证与售后服务承诺', 0.9968776702880859)],
    #     [[[543.0, 115.0], [731.0, 112.0], [731.0, 134.0], [543.0, 138.0]], ('附件12：乙方营业执照', 0.998201310634613)]
    # ]
    #
    # text_array = [[[[49.0, 50.0], [412.0, 63.0], [411.0, 92.0], [48.0, 79.0]], ('JT2024041003Z目称：宁乡紫云府', 0.9508424401283264)], [[[477.0, 63.0], [781.0, 60.0], [781.0, 81.0], [477.0, 84.0]], ('合同来源：战略集采中心材料名称：气体', 0.9967018961906433)], [[[126.0, 94.0], [287.0, 94.0], [287.0, 115.0], [126.0, 115.0]], ('附件4：采购入库单', 0.9982880353927612)], [[[542.0, 88.0], [768.0, 85.0], [768.0, 107.0], [542.0, 109.0]], ('附件11：《举报渠道告知函》', 0.9974693655967712)], [[[127.0, 122.0], [396.0, 118.0], [396.0, 140.0], [127.0, 143.0]], ('附件5：质量保证与售后服务承诺', 0.9968776702880859)], [[[543.0, 115.0], [731.0, 112.0], [731.0, 134.0], [543.0, 138.0]], ('附件12：乙方营业执照', 0.998201310634613)], [[[127.0, 148.0], [325.0, 146.0], [325.0, 167.0], [127.0, 170.0]], ('附件6《供应商承诺函》', 0.9997368454933167)], [[[543.0, 142.0], [823.0, 138.0], [823.0, 159.0], [543.0, 163.0]], ('附件13：合同终止最终结算对账表', 0.9995758533477783)], [[[128.0, 175.0], [290.0, 173.0], [290.0, 195.0], [128.0, 197.0]], ('附件7：《交接清单》', 0.9995840191841125)], [[[544.0, 170.0], [713.0, 166.0], [713.0, 189.0], [544.0, 192.0]], ('附件14：催款通知单', 0.999071478843689)], [[[544.0, 229.0], [863.0, 225.0], [863.0, 246.0], [544.0, 251.0]], ('乙方（盖章）：宁乡市腾达气体有限公司', 0.9951858520507812)], [[[129.0, 240.0], [431.0, 236.0], [431.0, 257.0], [129.0, 262.0]], ('甲方（盖章）：.长沙中联建设有限公司', 0.9692600965499878)], [[[131.0, 278.0], [220.0, 278.0], [220.0, 301.0], [131.0, 301.0]], ('甲方代表：', 0.9828791618347168)], [[[544.0, 273.0], [690.0, 271.0], [691.0, 292.0], [544.0, 295.0]], ('乙方代表：何桂罗', 0.9984330534934998)], [[[130.0, 344.0], [461.0, 339.0], [461.0, 362.0], [131.0, 366.0]], ('统一社会信用代码：91430100MA4M6RED6L', 0.988525927066803)], [[[544.0, 338.0], [876.0, 335.0], [876.0, 356.0], [544.0, 360.0]], ('统一社会信用代码：914301246707995959', 0.9974914193153381)], [[[132.0, 380.0], [153.0, 380.0], [153.0, 398.0], [132.0, 398.0]], ('电', 0.9999731779098511)], [[[182.0, 375.0], [339.0, 371.0], [339.0, 397.0], [182.0, 400.0]], ('话：45387529530', 0.9849452972412109)], [[[545.0, 372.0], [567.0, 372.0], [567.0, 392.0], [545.0, 392.0]], ('电', 0.9998606443405151)], [[[598.0, 371.0], [740.0, 371.0], [740.0, 393.0], [598.0, 393.0]], ('话：13467630996', 0.9992252588272095)], [[[131.0, 413.0], [153.0, 413.0], [153.0, 432.0], [131.0, 432.0]], ('地', 0.9999912977218628)], [[[182.0, 409.0], [531.0, 404.0], [531.0, 429.0], [182.0, 433.0]], ('址：中国湖南省长沙市宁乡县经开区城郊', 0.9985281825065613)], [[[544.0, 408.0], [565.0, 408.0], [565.0, 425.0], [544.0, 425.0]], ('地', 0.9988658428192139)], [[[597.0, 404.0], [944.0, 401.0], [945.0, 422.0], [597.0, 426.0]], ('址：湖南省宁乡市夏铎铺镇六度庵村（机械', 0.9902424216270447)], [[[130.0, 444.0], [497.0, 440.0], [498.0, 461.0], [130.0, 465.0]], ('街道为丰社区创业大道景途商业广场A栋3楼', 0.9504587054252625)], [[[543.0, 438.0], [611.0, 435.0], [612.0, 457.0], [543.0, 460.0]], ('工业园）', 0.9828952550888062)], [[[131.0, 477.0], [389.0, 474.0], [389.0, 495.0], [132.0, 498.0]], ('开户行：中国建设银行宁乡支行', 0.9985543489456177)], [[[544.0, 471.0], [911.0, 467.0], [911.0, 489.0], [544.0, 492.0]], ('开户行：中国建设银行股份有限公司宁乡支行', 0.9933775663375854)], [[[131.0, 511.0], [372.0, 508.0], [373.0, 529.0], [132.0, 533.0]], ('账号：43050178383600000146', 0.9992697238922119)], [[[545.0, 505.0], [787.0, 502.0], [787.0, 524.0], [545.0, 526.0]], ('账号：43001560061052507395', 0.9989719390869141)], [[[134.0, 544.0], [281.0, 544.0], [281.0, 565.0], [134.0, 565.0]], ('签约日期：2024-3', 0.9990772604942322)], [[[546.0, 539.0], [697.0, 537.0], [697.0, 559.0], [546.0, 561.0]], ('签约日期：2024-3', 0.9990271925926208)], [[[133.0, 577.0], [362.0, 574.0], [362.0, 595.0], [134.0, 599.0]], ('合同签订地：广州市番禺区', 0.9551052451133728)], [[[132.0, 608.0], [208.0, 608.0], [208.0, 631.0], [132.0, 631.0]], ('以下无文', 0.9988934397697449)]]

    filter_text_array = [item for item in text_array if item[1][1] >= 0.8];

    new_array = rearrange_text_by_line(filter_text_array)

    # 只获取内容不返回坐标
    # result_array = []
    # for line in new_array:
    #     item_line = '';
    #     for item in line:
    #         item_line = item_line + ' ' + item[1]
    #     result_array.append(item_line)

    sort_array = sort_by_first_coordinate(new_array)

    return sort_array


def sort_by_first_coordinate(data):
    # 定义一个提取第一个坐标点 x 值的函数
    def get_first_x(subarray):
        return subarray[0][0][0]  # 获取第一个坐标点的 x 值

    # 对每个子数组中的两个元素分别排序
    sorted_data = []
    for subarray in data:
        sorted_subarray = sorted(subarray, key=get_first_x)
        sorted_data.append(sorted_subarray)

    return sorted_data

if __name__ == "__main__":
    ocr_sort()